package com.library.control.book;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractFormController;

import com.library.model.dto.book.BookDTO;
import com.library.model.service.BookService;

public class BookInsertController extends AbstractFormController{

	private BookService bookservice;
	
	public void setBookservice(BookService bookservice) {
		this.bookservice = bookservice;
	}

	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
//		BookDTO dto=(BookDTO)super.formBackingObject(request);
		BookDTO dto= new BookDTO();
		String bookissue=request.getParameter("book_issue_y")+"-"+request.getParameter("book_issue_m")+"-"+request.getParameter("book_issue_d");
		String bookStoredDay=request.getParameter("book_stored_day_y")+"-"+request.getParameter("book_stored_day_m")+"-"+request.getParameter("book_stored_day_d");
		
		dto.setBook_issue(bookissue);
		dto.setBook_stored_day(bookStoredDay);
		return dto;
	}
	
	@Override
	protected ModelAndView processFormSubmission(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, BindException arg3)
			throws Exception {
		ModelAndView mav=new ModelAndView();
		bookservice.insertBook((BookDTO)arg2);
		mav.setViewName("bookinsertok");
		return mav;
	}

	@Override
	protected ModelAndView showForm(HttpServletRequest arg0,
			HttpServletResponse arg1, BindException arg2) throws Exception {
		return new ModelAndView("bookinsert");
	}

}
